package csu.web.mypetstore.util;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private int code;
    private String msg;
    private T data;
    private Result() {

    }
    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    @JsonIgnore
    public boolean isSuccess() {
        return code == StatusCode.SUCCESS.getCode();
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }


    public static <T> Result<T> success() {
        return new Result<T>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }
    public static <T> Result<T> success(String msg) {
        return new Result<T>(StatusCode.SUCCESS.getCode(), msg, null);
    }
    public static <T> Result<T> success(T data) {
        return new Result<T>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), data);
    }


    public static <T> Result<T> failure() {
        return new Result<T>(StatusCode.ERROR.getCode(),StatusCode.ERROR.getMessage(),null);
    }
    public static <T> Result<T> failure(String msg) {
        return new Result<T>(StatusCode.ERROR.getCode(), msg,null);
    }
    public static <T> Result<T> failure(int code,String msg) {
        return new Result<T>(code, msg,null);
    }




}
